package DispatchSys;

/**
 * The DuplicateDetection module, see Architecture report for specifications.
 * 
 */

public class DuplicateDetection {
	private Database db;

	/**
	 * Creates a module that detects duplicate emergencies
	 */
	public DuplicateDetection() {
		this.db = Database.getInstance();
	}

	/**
	 * PRE : er != null <br />
	 * 
	 * Check if the given emergency is already encoded. Here, the emergencies
	 * are the same if the location is exactly the same, and the notification
	 * time between the two emergencies is less than 15s (15min in real life) If
	 * needed, we can extend this for close locations and not only the exact
	 * same location
	 * 
	 * @param er
	 *            the emergency to be checked for duplicate
	 * 
	 * @return null if no duplicate found, the original Emergency if it is a
	 *         duplicate.
	 * 
	 */
	public ERInfo checkDuplicate(ERInfo er) {

		// Search in the list of emergencies a duplicate
		for (ERInfo current : db.getAllER()) {
			if (current.equals(er) && !current.isFinished()) {
				return current;
			}
		}

		// Search in the list of assignments, a duplicate
		for (Assignment current : db.getAllAssignments()) {
			if (current.getEr().equals(er)) {
				return current.getEr();
			}
		}

		return null;
	}
}
